﻿using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class GenerateResConfig : Editor
{
    //生成菜单
    [MenuItem("JEngine/Resources/Create mapping table")]//2>
    public static void Generate()
    {
        string path = Path.Combine(Application.dataPath, "Resources").Replace(@"\", "/");
        if (!Directory.Exists(path))
          Directory.CreateDirectory(path);
       
        string[] resFiles = Directory.GetFiles(path, "*.prefab", SearchOption.AllDirectories);

        string[] spritesFiles = Directory.GetFiles(path, "*.png", SearchOption.AllDirectories);
        
        string[] configFiles = Directory.GetFiles(path, "*.asset", SearchOption.AllDirectories);
        string[] jsonFiles = Directory.GetFiles(path, "*.json", SearchOption.AllDirectories);
        List<string> tempstrs =new List<string>();
        
        Dictionary<string, string> tempDic = new Dictionary<string, string>();
        
        for (int i = 0; i < jsonFiles.Length; i++)
        {
            string key = Path.GetFileNameWithoutExtension(jsonFiles[i]);
            string value = jsonFiles[i].Replace(@"\", "/").Replace(path + "/", "").Replace(".json", "");
            if (tempDic.ContainsKey(key))
            {
                EditorUtility.DisplayDialog("资源映射失败！", "资源名称重复："+key, "确定");
                return;
            }
            tempDic.Add(key,value);
            tempstrs.Add(key + "=" + value);
        }
        for (int i = 0; i < configFiles.Length; i++)
        {
            string key = Path.GetFileNameWithoutExtension(configFiles[i]);
            string value = configFiles[i].Replace(@"\", "/").Replace(path + "/", "").Replace(".asset", "");
            if (tempDic.ContainsKey(key))
            {
                EditorUtility.DisplayDialog("资源映射失败！", "资源名称重复："+key, "确定");
                return;
            }
            tempDic.Add(key,value);
            tempstrs.Add(key + "=" + value);
        }
        
        for (int i = 0; i < resFiles.Length; i++)
        {
            string key = Path.GetFileNameWithoutExtension(resFiles[i]);
            //resFiles[0]=path+"/Skill/BaseMeleeAttackSkill.prefab"
            string value = resFiles[i].Replace(@"\", "/").Replace(path + "/", "").Replace(".prefab", "");

            if (tempDic.ContainsKey(key))
            {
                Debug.LogError("资源映射失败！");
                //ResourcesTipsWindow.ShowTipsWindow(key);
                EditorUtility.DisplayDialog("资源映射失败！", "资源名称重复："+key, "确定");
                return;
            }
            
            tempDic.Add(key,value);
            
            //resFiles[i] = key + "=" + value;
            tempstrs.Add(key + "=" + value);
        }

        for (int i = 0; i < spritesFiles.Length; i++)
        {
            string value = spritesFiles[i].Replace(@"\", "/").Replace(path + "/", "").Replace(".png", "");

            Sprite[] atlas = Resources.LoadAll<Sprite>(value);

            string key = "";
            
            if (atlas.Length>1)
            {
                foreach (var obj in atlas)
                {
                    key = obj.name;

                    if (tempstrs.Contains(key + "=" + value))
                    {
                        Debug.LogError("资源映射失败！");
                        EditorUtility.DisplayDialog("资源映射失败！", "资源名称重复："+key, "确定");
                        return;
                    }
                    
                    tempstrs.Add(key + "=" + value);
                }
            }
            else
            {
                key =Path.GetFileNameWithoutExtension(spritesFiles[i]);
                
                tempstrs.Add(key + "=" + value);
            }
        }
        
        File.WriteAllLines(Path.Combine(path, "ResMap.txt"), tempstrs);
        AssetDatabase.Refresh();//3>
        Debug.Log("生成文件映射成功");
    }


   
}
